<?php

/**
 * Description of CmsController
 *
 * @author FFOZEU
 */
namespace Applications\Modules\Cms\Backend\Controller;

if( !defined('IN') ) die('Hacking Attempt');

use Library\BackController;
use Library\HttpRequest;
use Library\Classe\Form\Form;

class CmsController extends BackController{
    //put your code here
    
    public function executeCms(HttpRequest $request){
     
        $this->page->addVar('title', 'Contenu html');
        
        $manager = $this->managers->getManagerOf('Cms');
        
        $cms = $manager->getCms();
        
        $this->page->addVar('cmslist', $cms);
        
        $this->leftcolumn();
    }
    
    private function leftcolumn(){
        $out = array();
        $out['cms-page.html']= 'Liste contenu';
        $out['add-cms.html']= 'Ajouter une page';
        
        return $this->page->addVar('left_content', $out);
        
    }
    
    public function executeDelete(HttpRequest $request){
        
        $manager = $this->managers->getManagerOf('Cms');
        if($request->getExists('id')){
            $out['id'] = $request->getData('id');
            if($manager->deleteCms($out)){
                $this->errors = 'suppression réussie';
            }else{
                $this->errors = 'Echec lors de la suppression';
            }
            $this->app()->httpResponse()->redirect('cms-page.html');
            
        }
    }
    
        
    public function executeCreate(HttpRequest $request){
        $this->page->addVar('title', 'ajouter un contenu');
        $this->leftcolumn();
        $manager = $this->managers->getManagerOf('Cms');
        
        $cmsForm = new Form('addCms');
        $cmsForm->action($request->requestURI());
        //ajout des champs
        $cmsForm->add('Text', 'titre')
                ->label('Titre')
                ->required(true);
        
        $cmsForm->add('Textarea', 'texte')
                ->label('Contenu')
                ->add_class('field_text')
                ;
                
        if($request->getExists('id')){
            $cmsForm->add('Hidden', 'id')->value($request->getData('id'));
            $this->page->addVar('title', 'Modifier un contenu');
            $cmsForm->add('Submit', 'submit')->value('Modifier');
        }else{
            $cmsForm->add('Submit', 'submit')->value('Ajouter');
        }
        
        if($request->getMethod('post')){
           //test de la validation du post
            if ($cmsForm->is_valid($_POST)) {
                list($titre, $texte) = $cmsForm->get_cleaned_data('titre','texte');
                $_POST['texte'] = htmlspecialchars($texte);
                $_POST['date_insere'] = date('Y-m-d h:i:s');
                $_POST['date_maj'] = date('Y-m-d h:i:s');
                $_POST['titre'] = strip_tags($titre);
                if(!$request->getExists('id')){
                    if($manager->addCms($_POST)){
                        $this->app()->httpResponse()->redirect('cms-page.html');
                    }else{
                        $this->errors = 'Echec lors de l\'enregistrement';
                    }
                }else{
                    if($manager->updateCms($_POST)){
                        $this->app()->httpResponse()->redirect('cms-page.html');
                    }else{
                        $this->errors = 'Echec lors de la mise à jour';
                    }
                }
            }else{
                $cmsForm->bound($_POST);
            }
        }else{
            if($request->getExists('id')){
               $faq = $manager->getCmsById(intval($request->getData('id')));
               $faq_array['titre'] = $faq->getTitre();
               $faq_array['texte'] = html_entity_decode($faq->getTexte());
               $cmsForm->bound($faq_array);
            }else{
                $cmsForm->bound($_POST);
            }
        }
        $this->page->addVar('errors', $this->errors);
        $this->page->addVar('cmsForm', $cmsForm);  
    }
}

?>
